/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package address;


import java.util.List;
 
 
import org.hibernate.HibernateException;
import org.hibernate.Session;

 
import util.HibernateUtil;
 
public class AddressIpDAO extends HibernateUtil {
 
    public AddressIp add(AddressIp addressip) {
        System.out.println(addressip);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.saveOrUpdate(addressip);
        session.getTransaction().commit();
        return addressip;
    }
    
    public AddressIp deleteAddress(int id_address) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        AddressIp addressIp = (AddressIp) session.load(AddressIp.class, id_address);
        if(null != addressIp) {
            session.delete(addressIp);
        }
        session.getTransaction().commit();
        return addressIp;
    }

    public List<AddressIp> listAddress() {
         
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<AddressIp> listAddressIp = null;
        try {
             
            listAddressIp = (List<AddressIp>)session.createQuery("from AddressIp").list();
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return listAddressIp;
    }

    public AddressIp getAddressById(int addressId) {
        AddressIp addressIp = null;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        try {
            addressIp = (AddressIp) session.get(AddressIp.class, addressId);
            System.out.println(addressIp);
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return addressIp;
    }
    
}